/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import Enum.LoaiNguoiDungEnum;
import Enum.TrangThaiDonHangEnum;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.ChiTietHoaDon;
import pojo.ChiTietPhieuThu;
import pojo.HoaDon;
import pojo.NguoiDung;
import pojo.PhieuThu;
import pojo.TrangThaiDonHang;
import util.NewHibernateUtil;

/**
 *
 * @author 0
 */
public class PhieuThuDAO extends BaseDAO{
    public static List<PhieuThu> TimKiem(Integer maPhieuThu,String tenKhachHang,Date ngayLapPhieuFrom,Date ngayLapPhieuTo,Integer idCuaHang,NguoiDung nguoiDung) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string="from PhieuThu pt where (pt.isDelete=false or pt.isDelete=null)";
        if (nguoiDung.getLoaiNguoiDung().getId() != LoaiNguoiDungEnum.Admin.getCode()) {
            string += String.format(" and pt.cuaHang.id=%d", nguoiDung.getCuaHang().getId());
        }
        if(maPhieuThu!=null){
            string+=String.format(" and pt.id=%d", maPhieuThu);
        }
        if(!tenKhachHang.isEmpty()){
            string+=" and lower(pt.nguoiDung.tenNguoiDung) like :tenKhachHang";
        }
        if(ngayLapPhieuFrom!=null){
            string+=" and pt.ngayLapPhieu>=:ngayLapPhieuFrom";
        }
        if(ngayLapPhieuTo!=null){
            string+=" and pt.ngayLapPhieu<=:ngayLapPhieuTo";
        }
        if(idCuaHang!=null){
            string+=String.format(" and pt.cuaHang.id=%d",idCuaHang);
        }
        Query query=session.createQuery(string);
        if(!tenKhachHang.isEmpty()){
            query.setString("tenKhachHang", "%"+tenKhachHang.toLowerCase()+"%");
        }
        if(ngayLapPhieuFrom!=null){
            query.setDate("ngayLapPhieuFrom", ngayLapPhieuFrom);
        }
        if(ngayLapPhieuTo!=null){
            query.setDate("ngayLapPhieuTo", ngayLapPhieuTo);
        }
        List<PhieuThu> phieuThus=query.list();
        return phieuThus;
    }
    
    public static int Them(PhieuThu phieuThu,List<ChiTietPhieuThu> chiTietPhieuThus) throws Exception{
        int id=0;
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        Transaction transaction=session.beginTransaction();
        try{
            ArrayList<HoaDon> hoaDons=new ArrayList<>();
            id=(int) session.save(phieuThu);
            for(ChiTietPhieuThu chiTietPhieuThu:chiTietPhieuThus){
                chiTietPhieuThu.setPhieuThu(phieuThu);
                session.save(chiTietPhieuThu);
                ChiTietHoaDon chiTietHoaDon=chiTietPhieuThu.getChiTietHoaDon();
                chiTietHoaDon.setDaThanhToan(true);
                session.update(chiTietHoaDon);
                if(!hoaDons.contains(chiTietHoaDon.getHoaDon())){
                    hoaDons.add(chiTietHoaDon.getHoaDon());
                }
            }
            for(HoaDon hoaDon:hoaDons){
                long demChuaThanhToan=(long) session.createQuery(String.format("select count(cthd.id) from ChiTietHoaDon cthd where cthd.hoaDon.id=%d and cthd.daThanhToan=false",hoaDon.getId())).uniqueResult();
                if(demChuaThanhToan==0){
                    hoaDon.setTrangThaiDonHang(new TrangThaiDonHang(TrangThaiDonHangEnum.DaThanhToan.getCode()));
                }
                else {
                    hoaDon.setTrangThaiDonHang(new TrangThaiDonHang(TrangThaiDonHangEnum.ThanhToanMotPhan.getCode()));
                }
                session.update(hoaDon);
            }
            transaction.commit();
            session.close();
        }
        catch(HibernateException exception){
            transaction.rollback();
            session.close();
            throw new Exception(exception.getMessage());
        }
        return id;
    }
    
    public static List<PhieuThu> LayDanhSachBaoCaoHoaDon(Date ngayFrom, Date ngayTo, Integer idCuaHang) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        Query query=session.createQuery(String.format("from PhieuThu pt where pt.cuaHang.id=%d and pt.ngayLapPhieu>=:ngayFrom and pt.ngayLapPhieu<=:ngayTo order by pt.id",idCuaHang));
        query.setDate("ngayFrom", ngayFrom);
        query.setDate("ngayTo", ngayTo);
        List<PhieuThu> hoaDons=query.list();
        session.close();
        return hoaDons;
    }
}
